package com.biz.primus.ms.order.promotion;

import com.biz.primus.base.enums.PromotionTypeEnum;
import com.biz.primus.model.order.vo.OrderProductVO;
import com.biz.primus.model.order.vo.request.OrderRequestVO;
import com.biz.primus.model.promotion.vo.OrderPromotionDetailVo;
import com.biz.primus.model.promotion.vo.PurchaseGiftVo;
import com.biz.primus.ms.order.promotion.helper.OrderPromotionHelper;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 买赠
 * <p>PURCHASE_GIFT("买赠", "赠")</p>
 *
 * @author Tiger Feng
 * @date 2017年11月10日
 */
public class PurchaseGiftPromotionHandler extends AbstractPromotionHandler {
    private PurchaseGiftPromotionHandler() {
    }

    public PurchaseGiftPromotionHandler(
            OrderPromotionDetailVo orderPromotionDetailVo,
            OrderRequestVO orderRequestVO,
            OrderPromotionHelper orderPromotionHelper) {
        super(orderPromotionDetailVo, orderRequestVO, orderPromotionHelper);
    }

    @Override
    public boolean checkPromotionType() {
        return PromotionTypeEnum.PURCHASE_GIFT == orderPromotionDetailVo.getPromotionType();
    }

    @Override
    public void count() {
        // 赠送信息
        PurchaseGiftVo purchaseGift = orderPromotionDetailVo.getPurchaseGift();
        // 没有赠送信息，则不处理
        if (purchaseGift == null) {
            return;
        }
        // 可选赠送商品列表
        List<OrderProductVO> optionalComplimentaryProducts = new ArrayList<>();
        if (purchaseGift.getGiftProductIds() != null
                && !purchaseGift.getGiftProductIds().isEmpty()) {
            for (Long productId : purchaseGift.getGiftProductIds()) {
                optionalComplimentaryProducts.add(new OrderProductVO(productId));
            }
        }
        // 将可赠送商品ID数组转Set
        Set<Long> optionalProducts = new HashSet<>();
        optionalProducts.addAll(purchaseGift.getGiftProductIds());

        // 设置可选赠送商品列表
        orderRequestVO.setOptionalComplimentaryProducts(optionalComplimentaryProducts);
        // 赠送商品数量
        orderRequestVO.setComplimentaryQuantity(purchaseGift.getGiftProductQuantity());

        // 赠送商品数量为0
        if (orderRequestVO.getComplimentaryQuantity() == null
                || orderRequestVO.getComplimentaryQuantity() == 0) {
            orderRequestVO.setOptionalComplimentaryProducts(null);
            orderRequestVO.setSelectedComplimentaryProducts(null);
        }
        // 检查已选赠送商品数量是否在 赠送商品数量内
        if (orderRequestVO.getSelectedComplimentaryProducts() != null
                && !orderRequestVO.getSelectedComplimentaryProducts().isEmpty()) {
            // 过滤掉不再赠送商品列表内的商品
            orderRequestVO.setSelectedComplimentaryProducts(
                    orderRequestVO.getSelectedComplimentaryProducts()
                            .stream()
                            .filter(x -> optionalProducts.contains(x.getProductId()))
                            .collect(Collectors.toList())
            );
        }
    }
}
